/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;

/**
 * this class handles the user initiated requests to create access control
 * lists.
 *
 * @author Anton Strack
 */
public class AdministrationController implements CsdwaController {

    private Connection conn;
    private final String VIEW_LIST = "/index.jsp";
    private final String VIEW_VIEW = "/view.jsp";
    private final String VIEW_CREATE = "/create_acl.jsp";
    private final String VIEW_UPDATE = "/update.jsp";
    private final String VIEW_DELTE = "/delete.jsp";

    public AdministrationController(Connection conn) {
        this.conn = conn;
    }

    /**
     * This method handles get requests. It accepts the primaryResouce and
     * primaryAction as variables to allow routing from the handleGetRequest
     * method.
     *
     * @param primaryResource the primary resource that will handle the request
     * @param primaryAction the primary action on the resource.
     * @param request the request object
     * @return
     */
    public CsdwaRequest handleGetRequest(CsdwaRequest request) {
        String resource = request.getPrimaryResourceTypeName();
        String action = request.getPrimaryActionTypeName();

        if ("accesscontrollist".equalsIgnoreCase(resource)) {
            if ("create".equalsIgnoreCase(action)) {
                RoleDao rdao = new RoleDao(conn);
                rdao.selectBeans();
                request.setAttribute("roles", rdao.getList());
                ActionDao adao = new ActionDao(conn);
                adao.selectBeans();
                request.setAttribute("actions", adao.getList());
                ResourceTypeDao rtdao = new ResourceTypeDao(conn);
                rtdao.selectBeans();
                request.setAttribute("resourceTypes", rtdao.getList());
                request.setView(VIEW_CREATE);
            }
        }

        return request;
    }

    public CsdwaRequest handlePostRequest(CsdwaRequest request) {
        String requestResultMessage;
        String form = request.getParameter("formSubmit");
        System.out.println("entered handle post");
        if (!form.isEmpty()) {
            // login-form processing is handled earlier - in authenitcator
            if (form.equalsIgnoreCase("create-accesscontrollist")) {
                String role = request.getParameter("role");
                // String action = request.getParameter("action");
                String[] actions = request.getRequest().getParameterValues("action");

                // String resourceType = request.getParameter("resourceType");
                String[] resourceTypes = request.getRequest().getParameterValues("resourceType");
                String ownership = request.getParameter("owned");
                String access = request.getParameter("access");
                String returnForm = request.getParameter("returnToFormOnSuccess");
                int result = 0;
                if (!role.isEmpty() && !actions[0].isEmpty() && !resourceTypes[0].isEmpty() && !ownership.isEmpty() && !access.isEmpty()) {
                    Acl acl;
                    AclDao adao = new AclDao(conn);
                    // loop though each resourceType that was selected.
                    for (String resourceType : resourceTypes) {
                        // loop though all selected actions for the current resourceType
                        for (String action : actions) {
                            acl = new Acl(Integer.parseInt(role), Integer.parseInt(action), Integer.parseInt(resourceType), Integer.parseInt(ownership), true);
                            result = adao.insertBean(acl);
                        }
                    }
                    if (returnForm != null && returnForm.isEmpty() == false) {
                        request = this.handleGetRequest(request);
                    }
                    if (result >= 1) {
                        requestResultMessage = "The ACL entry was successfully saved to the database.";
                    } else {
                        requestResultMessage = "There was an error saving the ACL entry to the database.";
                    }

                } else {
                    requestResultMessage = "There was an error processing the form.";
                }
            } else {
                requestResultMessage = "There was an error processing the form.";
            }
            request.setAttribute("requestResultMessage", requestResultMessage);
            request.removeAttribute("source");
        }
        return request;
    }

    @Override
    public CsdwaRequest handleRequest(CsdwaRequest request) {
        if ("POST".equalsIgnoreCase(request.getMethod()) && request.getParameter("formSubmit") != null) {
            return this.handlePostRequest(request);
        } else {
            String resource = request.getPrimaryResourceTypeName();
            if (resource == null || resource.isEmpty()) {
                resource = "acl";// default resource // TEMP as acl, but change to AdminDashboard eventually
            }
            String action = request.getPrimaryActionTypeName();
            if (action == null || action.isEmpty()) {
                action = "create";// default resource // TEMP as create, but change to index eventually
            }
            return this.handleGetRequest(request);
        }
    }
}
